package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.User;
import com.itheima.reggie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
public class UserController {

    @Autowired
    private HttpSession httpSession;

    @Autowired
    private UserService userService;

    @PostMapping("/user/sendMsg")
    public ResultInfo sendMsg(@RequestBody Map<String,String> map){
        //这里进行验证码的发送，得到一个验证码3
//        System.out.println(map.get("phone"));
        String phone = map.get("phone");
        String code = "123";
        httpSession.setAttribute("SMS_" + phone, code);
        return ResultInfo.success(code);
    }

    @PostMapping("/user/login")
    public ResultInfo login(@RequestBody Map<String,String> map){
       String phone =  map.get("phone");
       String code =  map.get("code");
       User user =  userService.login(phone,code);
       return ResultInfo.success(user);
    }

    @PostMapping("/user/logout")
    public ResultInfo logout(){
        //删除session中的信息
        UserHolder.remove();
        httpSession.invalidate();
        return ResultInfo.success(null);
    }





}
